package org.celery.mobile.controller;

import lombok.AllArgsConstructor;
import org.celery.mobile.vo.MerchantVo;
import org.celery.modules.merchant.entity.Merchant;
import org.celery.modules.merchant.service.IMerchantService;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Create on 2021-12-07
 *
 * @author Celery <1031868928@qq.com>
 */
@RestController
@AllArgsConstructor
@RequestMapping("/mobile/common")
public class MobileCommonController {

    private final IMerchantService merchantService;

    @GetMapping(value = "merchant-list")
    public R<List<MerchantVo>> merchantList() {
        List<Merchant> merchantList = merchantService.list();

        List<MerchantVo> merchantVoList = new ArrayList<>();
        merchantList.forEach(merchant -> {
            Map<String, Object> address = JsonUtil.toMap(merchant.getLongitudeAndLatitude());
            merchantVoList.add(new MerchantVo() {{
                setId(merchant.getId());
                setCode(merchant.getCode());
                setAddress(merchant.getAddress());
                setLongitude(Func.isEmpty(address.get("longitude")) ? "" : address.get("longitude").toString());
                setLatitude(Func.isEmpty(address.get("latitude")) ? "" : address.get("latitude").toString());
                setContactNumber(merchant.getContactNumber());
                setPictureUrl(merchant.getPictureUrl());
                setName(merchant.getName());
            }});
        });
        return R.data(merchantVoList);
    }
}
